package com.usefullc.system.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.usefullc.common.exception.BizException;
import com.usefullc.common.util.BeanUtils;
import com.usefullc.system.domain.AppDevice;
import com.usefullc.system.domain.AppVersion;
import com.usefullc.system.entity.Pager;
import com.usefullc.system.mapper.AppVersionMapper;
import com.usefullc.system.query.AppVersionQuery;
import com.usefullc.system.query.BaseQuery;
import com.usefullc.system.vo.AppVersionVo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class AppVersionService extends AbstractBizService<AppVersionMapper,AppVersion,AppVersionQuery> {




    @Override
    public QueryWrapper<AppVersion> getQueryWrapper(AppVersionQuery query) {
        QueryWrapper<AppVersion> queryWrapper = super.getQueryWrapper(query);
        return queryWrapper;
    }

    public Pager<AppVersionVo> findVoByPage(AppVersionQuery query){
        Pager<AppVersion> page = this.findByPage(query);
        Pager<AppVersionVo> newPage = new Pager(page);
        List<AppVersion> dataList = page.getDataList();
        if (CollectionUtils.isNotEmpty(dataList)) {
            List<AppVersionVo> appVersionVos = BeanUtils.listBeanCopy(dataList, AppVersionVo.class);
            newPage.setDataList(appVersionVos);

            for (AppVersionVo appVersionVo : appVersionVos) {
                String updateLog = appVersionVo.getUpdateLog();
                String[] strings = updateLog.split("\\^\\#\\^");
                List<String> logItems = Arrays.asList(strings);
                appVersionVo.setUpdateLogItems(logItems);
            }
        }
        return newPage;
    }

    public AppVersion hasNewVersion(AppVersionQuery query){
//        AppVersionQuery query = new AppVersionQuery();
//        query.setAppName(appName);
//        query.setAppName(type);
//        query.setPlatform(platform);
        String version = query.getVersion();
        String versionNo = StringUtils.replaceAll(version,"\\.", "");
        query.setVersionNoStart(Integer.valueOf(versionNo));
        query.setVersion(null);
        query.setResultLimit(1);
        AppVersion appVersion = this.findOne(query);
        if(appVersion == null){
            throw new BizException("您已是最新版本",false);
        }
        return appVersion;
    }

}
